﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Neovolve.Windows.Forms.Controls;

namespace RLFtrClc
{
    public partial class CalculatedFighter : WizardPage
    {
        string _fighterType;
        Fighters _fighterGroup;
        ICalculatedFighter _calculatedFighter;

        public CalculatedFighter(string FighterType, Fighters FighterGroup, ICalculatedFighter CalculatedFighter)
        {
            InitializeComponent();
            _fighterType = FighterType;
            _fighterGroup = FighterGroup;
            _calculatedFighter = CalculatedFighter;            
            this.Opening += new EventHandler(CalculatedFighter_Opening);
            this.Closing += new EventHandler(CalculatedFighter_Closing);
        }

        void CalculatedFighter_Closing(object sender, EventArgs e)
        {
            clearArmorBoxes();
            switch (_fighterType)
            {
                case "Squadron":
                    _calculatedFighter = new CalculatedFighterSquadron();
                    break;
                case "Flight":
                    _calculatedFighter = new CalculatedFighterFlight();
                    break;
                case "Group":
                    _calculatedFighter = new CalculatedFighterGroup();
                    break;
            }
        }

        private void clearArmorBoxes()
        {
            string controlName = String.Empty;
            for (int i = 1; i < 73; i++)
            {
                controlName = "textBox" + i.ToString();
                this.Controls[0].Controls[controlName].BackColor = Color.White;
            }
        }

        void CalculatedFighter_Opening(object sender, EventArgs e)
        {
            init();
        }

        private void init()
        {
            setMovement();
            setMissiles();
            setCRA();
            setLRA();
            setArmor();
        }

        private void setArmor()
        {
            _fighterGroup.CreateArmor(_fighterType, _calculatedFighter);
            switch (_fighterType)
            {
                case "Squadron":
                    blockOutNonGroupBoxes();
                    blockOutNonFlightBoxes();
                    blockOutArmor();
                    break;
                case "Flight":
                    blockOutNonGroupBoxes();
                    blockOutArmor();
                    break;
                case "Group":
                    blockOutArmor();
                    break;
            }
        }

        private void blockOutArmor()
        {
            switch (_fighterType)
            {
                case "Squadron":
                    int armorRowOne = _calculatedFighter.getArmorRow(1);
                    if (armorRowOne < 6)
                    {
                        blockOutRange(7, 6 - armorRowOne);
                    }
                    break;
                case "Flight":
                    if (_calculatedFighter.getArmorRow(1) < 6) blockOutRange(7, 6 - _calculatedFighter.getArmorRow(1));
                    if (_calculatedFighter.getArmorRow(2) < 8) blockOutRange(15, 8 - _calculatedFighter.getArmorRow(2));
                    if (_calculatedFighter.getArmorRow(3) < 10) blockOutRange(25, 10 - _calculatedFighter.getArmorRow(3));
                    break;
                case "Group":
                    if (_calculatedFighter.getArmorRow(1) < 6) blockOutRange(7, 6 - _calculatedFighter.getArmorRow(1));
                    if (_calculatedFighter.getArmorRow(2) < 8) blockOutRange(15, 8 - _calculatedFighter.getArmorRow(2));
                    if (_calculatedFighter.getArmorRow(3) < 10) blockOutRange(25, 10 - _calculatedFighter.getArmorRow(3));
                    if (_calculatedFighter.getArmorRow(4) < 13) blockOutRange(38, 13 - _calculatedFighter.getArmorRow(4));
                    if (_calculatedFighter.getArmorRow(5) < 16) blockOutRange(54, 16 - _calculatedFighter.getArmorRow(5));
                    if (_calculatedFighter.getArmorRow(6) < 19) blockOutRange(73, 19 - _calculatedFighter.getArmorRow(6));
                    break;
            }
        }

        private void blockOutRange(int start, int blocks)
        {
            string controlName = String.Empty;
            for (int i = blocks; i > 0; i--)
            {
                controlName = "textBox" + (start - i).ToString();
                this.Controls[0].Controls[controlName].BackColor = Color.Black;
            }
        }

        private void blockOutNonGroupBoxes()
        {
            string controlName = String.Empty;
            for (int i = 25; i < 73; i++)
            {
                controlName = "textBox" + i.ToString();
                this.Controls[0].Controls[controlName].BackColor = Color.Black;
            }
        }

        private void blockOutNonFlightBoxes()
        {
            string controlName = String.Empty;
            for (int i = 7; i < 25; i++)
            {
                controlName = "textBox" + i.ToString();
                this.Controls[0].Controls[controlName].BackColor = Color.Black;
            }
        }

        private void setLRA()
        {
            _fighterGroup.CreateLRA(_fighterType, _calculatedFighter);
            switch (_fighterType)
            {
                case "Squadron":
                    txtLRAOne.Text = _calculatedFighter.getLRARow(1).ToString();
                    break;
                case "Flight":
                    txtLRAOne.Text = _calculatedFighter.getLRARow(1).ToString();
                    txtLRATwo.Text = _calculatedFighter.getLRARow(2).ToString();
                    txtLRAThree.Text = _calculatedFighter.getLRARow(3).ToString();
                    break;
                case "Group":
                    txtLRAOne.Text = _calculatedFighter.getLRARow(1).ToString();
                    txtLRATwo.Text = _calculatedFighter.getLRARow(2).ToString();
                    txtLRAThree.Text = _calculatedFighter.getLRARow(3).ToString();
                    txtLRAFour.Text = _calculatedFighter.getLRARow(4).ToString();
                    txtLRAFive.Text = _calculatedFighter.getLRARow(5).ToString();
                    txtLRASix.Text = _calculatedFighter.getLRARow(6).ToString();
                    break;
            }
        }

        private void setCRA()
        {
            _fighterGroup.CreateCRA(_fighterType, _calculatedFighter);
            switch (_fighterType)
            {
                case "Squadron":
                    txtCRAOne.Text = _calculatedFighter.getCRARow(1).ToString();
                    break;
                case "Flight":
                    txtCRAOne.Text = _calculatedFighter.getCRARow(1).ToString();
                    txtCRATwo.Text = _calculatedFighter.getCRARow(2).ToString();
                    txtCRAThree.Text = _calculatedFighter.getCRARow(3).ToString();
                    break;
                case "Group":
                    txtCRAOne.Text = _calculatedFighter.getCRARow(1).ToString();
                    txtCRATwo.Text = _calculatedFighter.getCRARow(2).ToString();
                    txtCRAThree.Text = _calculatedFighter.getCRARow(3).ToString();
                    txtCRAFour.Text = _calculatedFighter.getCRARow(4).ToString();
                    txtCRAFive.Text = _calculatedFighter.getCRARow(5).ToString();
                    txtCRASix.Text = _calculatedFighter.getCRARow(6).ToString();
                    break;
            }
        }

        private void setMovement()
        {
            _fighterGroup.CreateThrust(_fighterType, _calculatedFighter);
            txtMovement.Text = _calculatedFighter.getMovement().ToString();
        }

        private void setMissiles()
        {
            _fighterGroup.CreateMissiles(_calculatedFighter);
            txtMissiles.Text = _calculatedFighter.getMissiles().ToString();
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            printForm1.Form = this.FindForm();
            printForm1.Print();
        }
    }
}
